package webSpider.MyScrapy;

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;  
import org.apache.http.client.methods.HttpRequestBase;  
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;  
  
/** 
 * http������� 
 *  
 * @author mingyuan 
 *  
 */  
public abstract class RequestCommons {  
    protected HttpClient httpclient = null;  
  
    public RequestCommons() {  
        initHttpClient();  
    }  
  
    /** 
     * ��ʼ��httpclient 
     */  
    protected void initHttpClient() {  
    	HttpHost proxy = new HttpHost("localhost",8888);
//		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//        cm.setMaxTotal(10);//���ӳ���󲢷�������
//        cm.setDefaultMaxPerRoute(10);//��·����󲢷���
//        httpclient = HttpClients.custom().setProxy(proxy).setConnectionManager(cm).build();
    	  httpclient = HttpClients.custom().setProxy(proxy).build();
    }  
  
    protected HttpClient getHttpClient() {  
        return httpclient;  
    }  
  
    protected void addHeader(HttpRequestBase request, String key, String value) {  
        request.addHeader(key, value);  
    }  
  
    protected void addCookie(HttpRequestBase request, String cookie) {  
        addHeader(request, "Cookie", cookie);  
    }  
  
    protected void setCookie(HttpRequestBase request, String cookie) {  
        request.setHeader("Cookie", cookie);  
    }  
  
    /** 
     * ���������headerֵ 
     *  
     * @param request 
     *            http��get����post���� 
     */  
    protected void setHeader(HttpRequestBase request) {  
        request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setHeader("Accept-Encoding", "gzip,deflate");
        request.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");  
        request.setHeader("Connection", "keep-alive");  
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");  
    }  
}  